import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String []a = new String[n];
		for(int i=0;i<n;i++)
		{
			a[i] = sc.next();
		}
		while(true)
		{
			String choose = sc.next();
			if(choose.equals("sort1"))
			{
				List<String> s1 = new ArrayList();
				for (int i = 0; i < a.length; i++)
				{
					s1.add(a[i].substring(6, 10) + "-" + a[i].substring(10, 12) + "-" + a[i].substring(12, 14));
				}
				Collections.sort(s1);
				for(String temp:s1)//???
				{
					System.out.println(temp);
				}
			}
			else if(choose.equals("sort2"))
			{
				String s2[] = new String[a.length];
				for (int i = 0; i < s2.length; i++) 
				{
					s2[i] = a[i].substring(6,14);
				}
				Sort(s2,a);
			}
			else System.out.println("exit");		
		}
	}
	private static void Sort(String[] s, String[] str) {
		int minIndex=0;
	    int temp=0;
	    String s3;
	    if((s==null)||(s.length==0))
	        return;
	    int b[] = new int [s.length];
	    for (int i = 0; i < b.length; i++)
	    {
	    	 b[i] = Integer.valueOf(s[i]);
		}
	    
	    for(int i=0;i < b.length-1;i++)//???
	    {
	        minIndex=i;
	        for(int j=i+1;j<s.length;j++)
	        {
	            if(b[j] < b[minIndex])
	            {
	                minIndex=j;
	            }
	        }
	        if(minIndex!=i)
	        {
	            temp=b[i];
	            s3 = str[i];
	            b[i]=b[minIndex];
	            str[i] = str[minIndex];
	            b[minIndex]=temp;
	            str[minIndex] = s3;
	        }
	    }
		for (int i = 0; i < str.length; i++) {
			System.out.println(str[i]);
		}
	}
}